      SUBROUTINE QUIKVIS5B(IDTARG,TARGNAMES,KTARGTYP,TARGPARM,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE IS PART OF THE QUIKVIS PROGRAM.  IT IS THE DRIVER FOR
C COMPUTING THE TARGET AVAILABILITY FOR THE GRID OF TARGETS IN THE
C SURVEY OPTION.
C
C
C VARIABLE      DIM       TYPE I/O DESCRIPTION
C --------      ---       ---- --- -----------
C
C IDTARG      MAXTARGS     I*4  I  DESCRIBED IN QUIKVIS(=MAIN) PROLOGUE.
C
C TARGNAMES   MAXTARGS    CH*16 I  DESCRIBED IN QUIKVIS(=MAIN) PROLOGUE.
C
C KTARGTYP    MAXTARGS     I*4  I  DESCRIBED IN QUIKVIS(=MAIN) PROLOGUE.
C
C TARGPARM NPARMS,MAXTARGS R*8  I  DESCRIBED IN QUIKVIS(=MAIN) PROLOGUE.
C
C IERR           1         I*4  O  ERROR RETURN FLAG
C                                  =0, NO ERROR
C                                  =OTHERWISE, ERROR.
C
C***********************************************************************
C
C BY C PETRUZZO/GFSC/742.   2/86.
C       MODIFIED....
C
C***********************************************************************
C
      INCLUDE 'QUIKVIS.INC'
C
      CHARACTER*16 TARGNAMES(MAXTARGS)
      INTEGER*4 IDTARG(MAXTARGS)
      REAL*8 TARGPARM(NPARMS,MAXTARGS)
      INTEGER*4 KTARGTYP(MAXTARGS)
      REAL*8 VISCURR(MAXTARGS),VISCUMM(MAXTARGS)
C
C
      REAL*8 SUNPOS(3),ELEMS(6),UTARG(3)
      CHARACTER*18 DATETIME
      LOGICAL GRIDNOW,DOCURR,DOCUMM
C
      IBUG = 0
      LUBUG = 19
C
C ERROR CHECK.  IS PARAMETER MAXTARGS SET OK IN QUIKVIS.INC ?
C
      IF(MAXTARGS.LT.NRASURVEY*NDECSURVEY)
     *   STOP ' QUIKVIS5B. CODING ERROR. STOPPED. SEE SOURCE CODE.'
C
C
      IF(IBUG.NE.0) WRITE(LUBUG,9001)
     *   (ITARG,TARGPARM(1,ITARG)*DEGRAD,TARGPARM(2,ITARG)*DEGRAD,
     *      ITARG=1,80)
 9001 FORMAT(/,' QUIKVIS5B. DEBUG. ENTRY VALUES.'/,
     *        ('    ITARG=',I3,'  RA,DEC=',2G13.5))
C
C
C ****************
C *  INITIALIZE  *
C ****************
C
      IERR = 0
      ELEMS(1) = SMA
      ELEMS(2) = ECC
      ELEMS(3) = ORBINCL
      ELEMS(5) = ARGP
      ELEMS(6) = 0.D0
      DELRA = TWOPI/(NRASURVEY-1)
      DELDEC = PI/(NDECSURVEY-1)
      CALL MTXSETR8(VISCUMM,1.D10,MAXTARGS,1)
C    KSVYOUT1 ASSIGNMENTS: (1=CURRENT DATE ONLY; 2=CUMULATIVE DATES ONLY
C                             ELSE=BOTH)
      DOCURR = KSVYOUT1.NE.2
      DOCUMM = KSVYOUT1.NE.1
C
C
C ********************************************
C *  STEP ALONG THE TIME PERIOD OF INTEREST  *
C ********************************************
C
      DO 2000 ITIME=1,NUMTIMES
C
      IF(INTERACTIVE) THEN
        WRITE(LUPROMPT,9002) ITIME,NUMTIMES,DATETIME(0)
 9002   FORMAT(' START PROCESSING TIME NUMBER ',I3,' OF ',I3,' AT ',A)
        END IF
C
      T50 = TSTART + (ITIME-1) * DELTIME
      IF(DOREQMT(2)) CALL SOLM50(T50,SUNPOS,0)
      CALL MTXSETR8(VISCURR,1.D10,MAXTARGS,1)
C
C    SET THE RAAN RANGE.  IT IS DONE INSIDE THE ITIME LOOP IN CASE THE
C    RAAN'S ARE DEFINED IN TERMS OF MEAN SOLAR TIME.
      CALL QUIKVIS5Y(T50,FIRSTNODE,NUMNODES,DELNODE)
C
C*** STEP ALONG THE RAAN RANGE
C
      DO 3000 INODE=1,NUMNODES
      ELEMS(4) = FIRSTNODE + (INODE-1) * DELNODE
C
C*** PROCESS EACH TARGET
C
      DO 1000 ITARG=1,NUMTARGS
C
C      ERROR CHECK. QUIKVIS5B IS FOR SKY SURVEY OPT. FIXED TARGS ONLY.
        IF(KTARGTYP(ITARG).NE.3) STOP 'QUIKVIS5B. CODING ERROR. STOP 1.'
C
C      WE NEED THIS TARGET'S AVAILABILITY INFO IF (1) THIS RUN IS GIVING
C      SURVEY INFO AT EACH TIME STEP OR IF (2) IT IS GIVING CUMULATIVE
C      INFO AND THIS TARGET'S MINIMUM AVAILABILITY IS GREATER THAN ZERO,
C      SO THAT EVEN LOWER AVAILABILITY TIME MAY BE ENCOUNTERED.
        IF(DOCURR .OR. (DOCUMM.AND.VISCUMM(ITARG).GT.0.D0)) THEN
          RATARG =   TARGPARM(1,ITARG)
          DECTARG =  TARGPARM(2,ITARG)
          UTARG(1) = TARGPARM(3,ITARG)
          UTARG(2) = TARGPARM(4,ITARG)
          UTARG(3) = TARGPARM(5,ITARG)
          CALL QUIKVIS5X(ELEMS,UTARG,SUNPOS,TAVAIL,.FALSE.,DUM,IERR)
          IF(IERR.NE.0) THEN
            IERR = 1
            GO TO 9999
            END IF
          IF(DOCURR) VISCURR(ITARG) = DMIN1(TAVAIL,VISCURR(ITARG))
          IF(DOCUMM) VISCUMM(ITARG) = DMIN1(TAVAIL,VISCUMM(ITARG))
          END IF
C
 1000 CONTINUE ! END ITARG LOOP
C
 3000 CONTINUE ! END INODE LOOP
C
      GRIDNOW = MOD(ITIME-1,KSVYFREQ).EQ.0  .OR.  ITIME.EQ.NUMTIMES
      IF(GRIDNOW) THEN
        IF(DOCURR)
     *      CALL QUIKVIS5B1(T50,T50,VISCURR,IDTARG,KTARGTYP,TARGPARM)
        IF(DOCUMM) THEN
          IF(.NOT.(DOCURR.AND.ITIME.EQ.1)) ! IE, IF NOT ALREADY WRITTEN
     *      CALL QUIKVIS5B1(TSTART,T50,VISCUMM,IDTARG,KTARGTYP,TARGPARM)
          END IF
        END IF
C
 2000 CONTINUE ! END ITIME LOOP
C
C
 9999 CONTINUE
      RETURN
C
C***********************************************************************
C
C
C**** INITIALIZATION CALL. PUT GLOBAL PARAMETER VALUES INTO THIS
C     ROUTINE'S LOCAL VARIABLES.
C
      ENTRY QVINIT5B
C
      CALL QUIKVIS999(-1,R8DATA,I4DATA,L4DATA)
      RETURN
C
C***********************************************************************
C
      END
